package io.github.biezhi.elves.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.github.pagehelper.Page;

import io.github.biezhi.elves.model.CompBasic;
import io.github.biezhi.elves.model.QccCookie;

public interface CompMapper {
	/**
	 * 查询企业表已更新logo数量数
	 * @param cityId
	 * @return
	 */
	@Select("select count(1) from t_comp_basic t where t.comp_city_id = #{cityId} and t.is_update_logo = 'T'")
	public int countCompBasicUpdateLogo(Integer cityId);
	
	/**
	 * 根据地市ID查询企业数据
	 * @param cityId
	 * @return
	 */
	@Select("select cb.COMP_ID as compId,cb.COMP_NAME as compName,cb.COMP_LOGO as compLogo,"
			+"cb.COMP_TELPHONE as compTelphone,cb.COMP_NET as compNet,"
			+"cb.COMP_ADDR as compAddr,cb.COMP_DESC as compDesc "
			+"from t_comp_basic cb "
			+"where cb.COMP_CITY_ID = #{cityId} "
			+"and cb.COMP_NAME != 'null' "
			+"and cb.IS_UPDATE_LOGO = 'F'")
	public Page<CompBasic> listCompBasicPageByCityId(@Param("cityId") Integer cityId);
	
	/**
	 * 根据企业名称和地市ID批量更新企业地址、logo、简介
	 * @param compName
	 * @return
	 */
	@Update("UPDATE t_comp_basic "
			+"set COMP_LOGO = #{compLogo},COMP_TELPHONE = #{compTelphone},"
			+"COMP_NET = #{compNet}, COMP_ADDR = #{compAddr},COMP_DESC = #{compDesc},"
			+"IS_UPDATE_LOGO = 'T' "
			+"where COMP_ID = #{compId}")
	public void updateCompBasicData(CompBasic compBasic);
	
	/**
	 * 查询企查查没有使用过的账户cookie数据
	 * @return
	 */
	@Select("select qcc_id,phone,qcc_cookie from t_qcc_cookie where is_use = 'F' limit 1;")
	@Results({
		@Result(column = "qcc_id", property = "qccId", id = true),
		@Result(column = "phone", property = "phone"),
		@Result(column = "qcc_cookie", property = "qccCookie")
	})
	public QccCookie findQccCookie();
	
	/**
	 * 根据id更新使用企查查账户cookie数据(更新is_use字段值为T)
	 * @param qccId
	 */
	@Update("update t_qcc_cookie set is_use = 'T' where qcc_id = #{qccId}")
	public void updateQccCookieIsUse(@Param("qccId") Integer qccId);
}